Author: SAI K
In Java, the String class provides multiple methods to check if a string is empty or consists only of whitespace. Two commonly used methods for this purpose are isEmpty()
and isBlank()
. Although they might seem similar, they serve different purposes and have distinct behavior. This blog post will explore the differences between isEmpty()
and isBlank()
.
The isEmpty()
method checks if a string has a length of 0. It returns true if the string is empty and false otherwise.
public boolean isEmpty()
public class IsEmptyExample {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = "Hello";
System.out.println("str1 is empty: " + str1.isEmpty()); // true
System.out.println("str2 is empty: " + str2.isEmpty()); // false
System.out.println("str3 is empty: " + str3.isEmpty()); // false
}
}
str1 is empty: true
str2 is empty: false
str3 is empty: false
The isBlank()
method checks if a string is empty or consists only of whitespace characters. It returns true if the string is empty or contains only whitespace characters, and false otherwise.
public boolean isBlank()
public class IsBlankExample {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = "\n\t";
String str4 = "Hello";
System.out.println("str1 is blank: " + str1.isBlank()); // true
System.out.println("str2 is blank: " + str2.isBlank()); // true
System.out.println("str3 is blank: " + str3.isBlank()); // true
System.out.println("str4 is blank: " + str4.isBlank()); // false
}
}
str1 is blank: true
str2 is blank: true
str3 is blank: true
str4 is blank: false
isEmpty()
returns false if the string contains any characters, including whitespace.isBlank()
returns true if the string contains only whitespace characters or is empty.isEmpty()
has been available since Java 6.isBlank()
was introduced in Java 11.public class IsEmptyUsage {
public static void main(String[] args) {
String input = " ";
if (input.isEmpty()) {
System.out.println("The input string is empty.");
} else {
System.out.println("The input string is not empty.");
}
}
}
public class IsBlankUsage {
public static void main(String[] args) {
String input = " ";
if (input.isBlank()) {
System.out.println("The input string is blank.");
} else {
System.out.println("The input string is not blank.");
}
}
}
The input string is not empty.
The input string is blank.
public class StringCheckExample {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = "Hello";
String str4 = "\t\n";
System.out.println("Using isEmpty() method:");
System.out.println("str1 is empty: " + str1.isEmpty());
System.out.println("str2 is empty: " + str2.isEmpty());
System.out.println("str3 is empty: " + str3.isEmpty());
System.out.println("str4 is empty: " + str4.isEmpty());
System.out.println("\nUsing isBlank() method:");
System.out.println("str1 is blank: " + str1.isBlank());
System.out.println("str2 is blank: " + str2.isBlank());
System.out.println("str3 is blank: " + str3.isBlank());
System.out.println("str4 is blank: " + str4.isBlank());
}
}
Using isEmpty() method:
str1 is empty: true
str2 is empty: false
str3 is empty: false
str4 is empty: false
Using isBlank() method:
str1 is blank: true
str2 is blank: true
str3 is blank: false
str4 is blank: true
The isEmpty()
and isBlank()
methods in Java are useful for checking whether a string is empty or contains only whitespace characters. While isEmpty()
checks if the string's length is 0, isBlank()
checks if the string is empty or consists only of whitespace characters. Understanding the differences between these methods helps in choosing the right method for various use cases.
Happy coding!